package com.manor.main.dialog

import android.content.Context
import android.text.TextUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.OnSelectListener
import com.lxj.xpopup.util.XPopupUtils
import com.manor.common.em.AuthType
import com.manor.common.event.AuthResultEvent
import com.manor.common.http.HttpCallback
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.RouteUtil
import com.manor.common.utils.ToastUtil
import com.manor.common.utils.str
import com.manor.common.utils.*
import com.manor.main.R
import com.manor.main.http.MainHttpUtil
import com.manor.paradise.bean.NickNameBean
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.dialog_agent_recharge.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.math.roundToInt

/**
 *
 *
 *
 * @date  2020/10/20  17:58
 * @version 1.0
 */
class AgentRechargePopup(context: Context, private val tips: String,private val listener: OnSelectListener)
    : CenterPopupView(context) {

    private var toId = ""


    companion object {
        fun load(context: Context, tip: String, listener: OnSelectListener) {
            XPopup.Builder(context)
                    .hasBlurBg(true)
                    .asCustom(AgentRechargePopup(context, tip,listener))
                    .show()
        }
    }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_agent_recharge
    }

    override fun onCreate() {
        super.onCreate()
        tvHint.text = tips
        tvGetNum.setOnClickListener { if (ClickUtil.canClick()) getToSdNum() }
        ivDismiss.setOnClickListener { dismiss() }
        tvConfirm.setOnClickListener { doConfirm() }
        EventBus.getDefault().register(this)
    }

    override fun onDismiss() {
        EventBus.getDefault().unregister(this)
        super.onDismiss()
    }

    private fun doConfirm() {
        if (toId.isEmpty()) {
            ToastUtil.show("请输入已认证用户的淘米号")
            return
        }
        if (etNum.str().isEmpty()) {
            ToastUtil.show("请输入代充嗨券数量")
            return
        }

        RouteUtil.forwardFaceExp(AuthType.TYPE_AGENT)


    }


    private fun getToSdNum() {
        if (!TextUtils.isEmpty(etSdId.text.toString())) {
            if (etSdId.text.toString().length > 5) {
                toId = ""
                tvNick.text = ""
                ElvesHttpUtil.getSearchNo(etSdId.text.toString(), object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {
                        if (code != 200) {
                            tvNick.text = msg
                            toId = ""
                            //   ToastUtil.show(msg)
                            return
                        }
                        var nick: NickNameBean = GsonUtil.gsonToBean(info, NickNameBean::class.java)
                        tvNick.text = nick.nickName.toString()
                        toId = nick.uid.toString()
                    }

                    override fun onError() {
                        super.onError()
                        tvNick.text = "没找到用户"
                        toId = ""
                    }
                })
            } else {
                ToastUtil.show("淘米号输入不正确")
                tvNick.text = ""
            }
        }

    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAuthResult(e: AuthResultEvent){
        if (e.type == AuthType.TYPE_AGENT)
        if (e.code == 200 && e.url.isNotEmpty()) {
            MainHttpUtil.getAgentRecharge(toId, etNum.str(), e.url, object : HttpCallback() {
                override fun onSuccess(code: Int, msg: String?, info: String?) {
                    if (code == 0){
                        ToastUtil.show("代充成功")
                        listener.onSelect(0,"")
                        dismiss()
                    } else {
                        msg?.let { ToastUtil.show(it) }
                    }
                }
            })
        }else{
            ToastUtil.show("检测失败")
        }
    }


    override fun getMaxHeight(): Int {
        return (XPopupUtils.getWindowHeight(context) * .80f).roundToInt()
    }

    override fun getMaxWidth(): Int {
        return (XPopupUtils.getWindowWidth(context) * .80f).roundToInt()
    }

}